package com.niodata.dp.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateParser {

  public static final String VARIABLE_DATE_START = "T";
  public static final String VARIABLE_DATE_NOW = "t";
  public static final String VARIABLE_EXEC_DIR = "d";
  public static final String VARIABLE_FTP_FILENAME = "F";
  public static final String VARIABLE_FTP_FILEPATH = "P";


  /**
   * program start time.
   */
  private static final Date DATE_START = new Date();
  private static final String EXEC_DIR = System.getProperty("user.dir");

  /**
   * current time.
   *
   * @return date
   */
  public static Date dateNow() {
    return new Date();
  }

  public static Date dateStart() {
    return DATE_START;
  }

  public static String dateStart(String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(DATE_START);
  }

  public static String getExecDir() {
    return EXEC_DIR;
  }


  /**
   * variable replace.
   *
   * @param source src pattern
   * @return replaced value
   */
  public static String replaceDateStartValue(String source) {
    return replaceDateStartValue(source, DATE_START);
  }

  /**
   * variable replace.
   *
   * @param source src pattern
   * @return replaced value
   */
  public static String replaceDateStartValue(String source, Date now) {
    if (StringUtil.isNotEmpty(source)) {
      //[T,H,-3,yyyy]
      Pattern pattern = Pattern.compile(getValuePattern());
      Matcher matcher = pattern.matcher(source);
      StringBuffer sbu = new StringBuffer();
      while (matcher.find()) {
        String matchStr = matcher.group();
        String exp = matchStr.substring(1, matchStr.length() - 1);
        String[] strs = exp.split(",");
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        if ("Y".equals(strs[1])) {
          cal.add(Calendar.YEAR, Integer.parseInt(strs[2]));
        } else if ("M".equals(strs[1])) {
          cal.add(Calendar.MONTH, Integer.parseInt(strs[2]));
        } else if ("D".equals(strs[1])) {
          cal.add(Calendar.DAY_OF_YEAR, Integer.parseInt(strs[2]));
        } else if ("H".equals(strs[1])) {
          cal.add(Calendar.HOUR, Integer.parseInt(strs[2]));
        } else if ("m".equals(strs[1])) {
          cal.add(Calendar.MINUTE, Integer.parseInt(strs[2]));
        }
        SimpleDateFormat sdf = new SimpleDateFormat(strs[3]);
        String value = sdf.format(cal.getTime());
        matcher.appendReplacement(sbu, value);
      }
      matcher.appendTail(sbu);
      return sbu.toString();
    } else {
      return source;
    }
  }

  public static Date getDateStart() {
    return DATE_START;
  }

  public static String getValuePattern() {
    return "\\[T,[Y,M,D,H,m],-?\\d+,[^\\]]*\\]";
  }

  public static boolean containDateTimeExpression(String expression) {
    if (StringUtil.isNotEmpty(expression)) {
      Pattern pattern = Pattern.compile(getValuePattern());
      Matcher matcher = pattern.matcher(expression);
      return matcher.find();
    } else {
      return false;
    }
  }
}
